package cn.com.java.service.impl;


import cn.com.java.mapper.SysRoleMenuMapper;
import cn.com.java.model.SysMenu;
import cn.com.java.model.SysRole;
import cn.com.java.model.SysRoleMenu;
import cn.com.java.service.SysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {


    @Override
    public List<Long> getSysRoleAndMenuByRoleId(Long roleId) {
        return this.baseMapper.getSysRoleAndMenuByRoleId(roleId);
    }

    @Override
    public void insertBySysRole(SysRole sysRole) {
        if (sysRole.getCheckedKeys().isEmpty()) {
            return;
        }
        //获取所有选中的菜单id
        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
        for (Long menuId : sysRole.getCheckedKeys()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(sysRole.getId());
            sysRoleMenuList.add(sysRoleMenu);
        }
        this.baseMapper.insertList(sysRoleMenuList);

    }

    @Override
    public void updateBySysRole(SysRole sysRole) {
        this.baseMapper.deleteById(sysRole.getId());
        if (sysRole.getCheckedKeys().isEmpty()) {
            return;
        }
        //获取所有选中的菜单id
        List<SysRoleMenu> sysRoleMenuList = new ArrayList<>();
        for (Long menuId : sysRole.getCheckedKeys()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(sysRole.getId());
            sysRoleMenuList.add(sysRoleMenu);
        }
        this.baseMapper.insertList(sysRoleMenuList);
    }

    @Override
    public List<SysMenu> getShowMenu() {
        //获取当前登录用户

        return null;
    }

}